﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DeskDict.Provider;
using System.Timers;

namespace DeskDict
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private IDictProvider provider;
        private Timer timer;
        private List<string> wordList;
        private Random random = new Random();

        public MainWindow()
        {
            InitializeComponent();

            provider = new XmlDictProvider();
            wordList = provider.GetWordList();

            timer = new Timer();
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Interval = 10000;
            timer.Start();
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (wordList.Count == 0)
            {
                return;
            }
            int index = random.Next(0, wordList.Count);
            WordDetail wordDetail = provider.Translate(wordList[index]);

            Word.Dispatcher.Invoke(new Action(
                delegate()
                {
                    Word.Text = wordDetail.ToString();
                }));
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Word_MouseEnter(object sender, MouseEventArgs e)
        {
            this.timer.Stop();
        }

        private void Word_MouseLeave(object sender, MouseEventArgs e)
        {
            this.timer.Start();
        }

        private void Top_Click(object sender, RoutedEventArgs e)
        {
          Top.IsChecked = !Top.IsChecked;
        }
    }
}
